package com.sanjiahantang.ythshopping.presenter;

import android.content.Context;

import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.config.StaticVelue;
import com.sanjiahantang.ythshopping.http.BaseDelegate;
import com.sanjiahantang.ythshopping.http.ExceptionHelper;
import com.sanjiahantang.ythshopping.http.OkHttpClientManager;
import com.sanjiahantang.ythshopping.model.CreateOrderModel;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.sanjiahantang.ythshopping.views.CreateOrderView;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sks on 2015/10/8.
 * 获取新的订单信息
 */
public class CreateOrderPresenter extends BasePresenter {
    private CreateOrderView createOrderView;

    public CreateOrderPresenter(CreateOrderView createOrderView){
        this.createOrderView = createOrderView;
    }

    public void loadOrderInfor(final Context context){
        initLoadDialog(context);
        mLoadingDialog.show();
        Map<String, String> params = new HashMap<>();
        params.put("key", ConfigValue.DATA_KEY);
        params.put("carts",getCarParam());


        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "order/confirm",
                params, new BaseDelegate.ResultCallback<CreateOrderModel>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        dismiss();
                        Utils.showToast(context, ExceptionHelper.getMessage(e, context));
                    }

                    @Override
                    public void onResponse(CreateOrderModel response, Object tag) {
                        dismiss();
                        createOrderView.getOrderInfor(response);
                    }
                },true);
    }

    public void loadOrderInforGoods(final Context context,String carid){
        initLoadDialog(context);
        mLoadingDialog.show();
        Map<String, String> params = new HashMap<>();
        params.put("key", ConfigValue.DATA_KEY);
        params.put("carts",carid);


        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "order/confirm",
                params, new BaseDelegate.ResultCallback<CreateOrderModel>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        dismiss();
                        Utils.showToast(context, ExceptionHelper.getMessage(e, context));
                    }

                    @Override
                    public void onResponse(CreateOrderModel response, Object tag) {
                        dismiss();
                        createOrderView.getOrderInfor(response);
                    }
                },true);
    }


    public String getCarParam()
    {
        String Carstr="";
        for (int i=0;i< StaticVelue.selectGoodsList.size();i++)
        {
            if (i!=StaticVelue.selectGoodsList.size()-1)
            {
                Carstr=Carstr+StaticVelue.selectGoodsList.get(i).getRec_id()+",";
            }else {
                Carstr=Carstr+StaticVelue.selectGoodsList.get(i).getRec_id();
            }
        }
        return Carstr;
    }

}
